package cn.china.xfood.controller;

import cn.china.xfood.serviceimpl.ProductServiceImpl;
import entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@RestController
/*...*/
public class MyController {
    @Autowired
    ListOperations<String,Object> listOperations;

    @Autowired
    ProductServiceImpl psi;
    @RequestMapping("/getProducts")
    public List<Product> getProducts(@RequestParam String type, @RequestParam Integer currentPage, HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin", "*");
        return psi.selectByType(type,currentPage);
    }


    @RequestMapping("/getCountNumByType")
    public int getCountNumByType(@RequestParam String type, HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin", "*");
        return psi.getCountNumByType(type);
    }

    @RequestMapping("/addCar")
    @Transactional
    public int addCar(@RequestParam Integer id,@RequestParam String url,@RequestParam String name,@RequestParam Double price,@RequestParam String username, HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin", "*");
        Product product=new Product();
         product.setId(id);
         product.setProductUrl(url);
         product.setProductName(name);
         product.setProductPrice(price);
        return psi.addCar(product,username);
    }



    @RequestMapping("/getCar")
    public List<Object> getCar(@RequestParam String username,HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin", "*");
        List<Object> list=new ArrayList<>();
        if(listOperations.size(username)>0){
            list=listOperations.range(username,0,listOperations.size(username)-1);
            return list;
        }

        return null;

    }

    @RequestMapping(value ="/upFile",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String upFile(@RequestParam("p_type") String p_type, @RequestParam("p_name") String p_name, @RequestParam("p_num") Integer p_num, @RequestParam("price") Double price, @RequestParam("intro") String intro, @RequestParam("picID") MultipartFile multipartFile, HttpServletResponse res) throws IOException, IOException, IOException {
        res.setHeader("Access-Control-Allow-Origin","*");

        String realPath = ResourceUtils.getURL("classpath:").getPath();
        String originalFilename=multipartFile.getOriginalFilename();//获取图片原始的名称
        System.out.println("=======我上传的图片名称是:"+originalFilename);

        long l=System.currentTimeMillis();//获取当前毫秒数
        String newName=l+originalFilename;
        System.out.println("=======毫秒数+原始名称:"+newName);

        InputStream inputStream=multipartFile.getInputStream();//输入流,用来读取图片

        System.out.println("路径"+realPath);

//在项目根目录下创建image文件夹.用来保存上传的图片
        File file=new File(realPath+"/static/images/");
        if(!file.exists()){
            file.mkdir();//如果这个文件不存在,创建文件夹
        }


        if(!multipartFile.isEmpty()){
            FileOutputStream fos=new FileOutputStream(realPath+"/static/images/"+newName);
            int length=0;
            while((length=inputStream.read())!=-1){
                fos.write(length);
            }
            fos.flush();
            fos.close();
            inputStream.close();

        }

        String url="http://localhost:8081/images/"+newName;
        Product product=new Product();
        product.setProductName(p_name);
        product.setProductAmount(p_num);
        product.setProductPrice(price);
        product.setProductType(p_type);
        product.setProductUrl(url);
        psi.insertSelective(product);

        return "ok";
    }





}
